package cn.cqone.crm.service.impl;

import cn.cqone.crm.domain.Employee;
import cn.cqone.crm.domain.SetMeal;
import cn.cqone.crm.domain.Tenant;
import cn.cqone.crm.mapper.EmployeeMapper;
import cn.cqone.crm.mapper.SetMealMapper;
import cn.cqone.crm.mapper.TenantMapper;
import cn.cqone.crm.query.SetMealQuery;
import cn.cqone.crm.service.SetMaelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SetMealServiceImpl extends BaseServiceImpl<SetMeal,Long, SetMealQuery>
    implements SetMaelService {

    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void buySetMeal(List<Long> ids, Employee employee) {
        List<SetMeal> setMeals = setMealMapper.selectFromIds(ids);
        String username = employee.getUsername();
        Long tenantId = employeeMapper.findTenantId(username);
        Tenant tenant = tenantMapper.findById(tenantId);
        setMeals.forEach(setMeal -> {
            setMeal.setTenant1(tenant);
        });
        setMealMapper.buySetMeal(setMeals);
    }

    @Override
    public int delete(Long aLong) {
        List<Long> length = setMealMapper.findLength(aLong);
        try {
            if (length.size()==0){
                return super.delete(aLong);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ArithmeticException("又删不该删的东西了吧");
        }
        return -1;
    }
}
